package net.labdemo.controller;

import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import net.labdemo.common.util.IqidoPage;
import net.labdemo.common.util.Page;
import net.labdemo.entity.Article;
import net.labdemo.entity.Refer;
import net.labdemo.entity.Role;
import net.labdemo.service.ArticleService;
import net.labdemo.service.ReferService;
import net.labdemo.service.UserService;

@Controller
@RequestMapping(value="/article")
public class ArticleController {
	@Autowired
	ArticleService articleService;
	@Autowired
	UserService userService;
	@Autowired
	ReferService referService;
	
	@ModelAttribute
	public Article get(@RequestParam(required=false) String id) {
		Article entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = articleService.get(Long.valueOf(id));
		}
		if (entity == null){
			entity = new Article();
		}
		return entity;
	}
	/*
	@InitBinder
	public void initBinder(WebDataBinder binder) {
		binder.registerCustomEditor(User.class, new UserEditor(userService));
	}
	*/
	@InitBinder
	public void initBinder(WebDataBinder binder) {
		binder.registerCustomEditor(Role.class, new RoleEditor());
	}
	@RequestMapping(value={"/list"})
	public String all(
			@RequestParam(value="p",required=false,defaultValue="1") int page,
			Model model){
		Page<Article> result=articleService.selectPage(new Article(), new Page<Article>(page,2));
		model.addAttribute("article",result.getResult());
		//分页
		IqidoPage ipObj=result.getIqidoPage("article/list", 10);
		//分页显示上限
		ipObj.setMaxPage(10);
		Map<String,String> pO=ipObj.draw(false);
		Map<Integer,String> pH=ipObj.drawPagination();
		model.addAttribute("pO", pO);
		model.addAttribute("pH", pH);

		return "article/list";
	}
	@RequestMapping(value="/publish",method=RequestMethod.GET)
	public String form(Article article,Model model){
		System.out.println("[AC]author:"+article.getAuthor());
		Collections.addAll(article.getRoles(), new Role(1, "公开"));
		model.addAttribute("article", article);
		model.addAttribute("authors", userService.all());
		model.addAttribute("roleList", Role.getDefined());
		return "article/form";
	}
	@RequestMapping(value="/publish",method=RequestMethod.POST)
	public String save(@ModelAttribute("article") Article articleForm,Model model){
		for(Role r:articleForm.getRoles()){
			System.out.println(">>"+r.getSymbol()+"<<"+r.getDescrip());
		}
		
		if(articleService.publish(articleForm,1)>0){
			return "redirect:/article/setting/"+articleForm.getId();
		}
		//测试
		//model.addAttribute("authors", userService.all());
		//model.addAttribute("roleList", Role.getDefined());
		return "article/form";
	}
	@RequestMapping(value="/show",method=RequestMethod.GET)
	public String query(Article article,Model model){
		//@1ArticleMapper.1.bak N+1关联User,(mybatis-config.xml Lazy)
		//@2ArticleMapper.2.bak 关联所有对象User+Set<Refer>(mybatis-config.xml Lazy)
		//@3通过一个单独的查询
		Set<Refer> refers=referService.getArticle(article.getId());
		article.setReferes(refers);
		model.addAttribute("article", article);
		
		return "article/get";
	}
	@RequestMapping(value="/setting/{id}",method=RequestMethod.GET)
	public String setting(@PathVariable("id") long articleId,Model model){
		Set<Refer> referes=referService.getArticle(articleId);
		model.addAttribute("referList", referes);
		model.addAttribute("articleId", articleId);
		return "article/option";
	}
	
	//测试json/xml
	@RequestMapping(value="/rest/{id}",method=RequestMethod.GET)
	@ResponseBody
	public Article selectEntity(@PathVariable("id")long articleId){
		return articleService.get(articleId);
	}
	@RequestMapping(value="/rest/most",method=RequestMethod.GET)
	@ResponseBody
	public List<Article> selectMostEntity(){
		Page<Article> result=articleService.selectPage(new Article(), new Page<Article>(1,10));
		return result.getResult();
	}

}
